package cn.edu.neusoft.controller;

import cn.edu.neusoft.config.PlatformConfig;
import cn.edu.neusoft.model.bean.Entrance;
import cn.edu.neusoft.model.bean.OverSpeed;
import cn.edu.neusoft.model.event.eventSubcribe.ReceiveAlarmMsg;
import cn.edu.neusoft.model.event.eventSubcribe.ReceiveMsg;
import cn.edu.neusoft.model.event.eventSubcribe.ReceiveCarIOMsg;
import cn.edu.neusoft.model.event.eventSubcribe.ReceiveCarOverSpeedMsg;
import cn.edu.neusoft.model.impl.EntranceInfoImpl;
import cn.edu.neusoft.model.impl.OverSpeedInfoImpl;
import cn.edu.neusoft.service.OauthHandler;
import com.dahuatech.hutool.json.JSONObject;
import com.dahuatech.hutool.json.JSONUtil;
import com.dahuatech.icc.exception.ClientException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


@Slf4j
//@RestController
public class SubscribeCallBackController {
//    @Autowired
//    private EntranceInfoImpl entranceInfo;
//    @Autowired
//    private OverSpeedInfoImpl overSpeedInfo;
//
//    @Autowired
//    private PlatformConfig platformConfig;
//
//    @Autowired
//    private OauthHandler oauthHandler;
//    /**
//     * 接收订阅回调消息
//     * @param data 接收消息参数
//     * @return 处理结果
//     */
//    @PostMapping("/receiveMsg")
//    public JSONObject receiveMsg(@RequestBody JSONObject data) throws IllegalAccessException, ClientException {
//        //打印原始返回数据
//        log.info("SubscribeCallBackController,receiveMsg,data:{}", data);
//        //TODO: 对收到的消息进行处理
//        //对数据进行类型转换, 注意这里需要根据实际返回数据进行类型转换
//        ReceiveMsg receiveMsgVO = JSONUtil.toBean(data, ReceiveMsg.class);
//
//        if(receiveMsgVO.getCategory().equals("alarm")){
//            ReceiveAlarmMsg receiveAlarmMsg = JSONUtil.toBean(receiveMsgVO.getInfo(), ReceiveAlarmMsg.class);
//            //https://open-icc.dahuatech.com/#/home?url=%3Fnav%3Dwiki%2Fevo-event%2Fsubscribe.html%23%E9%80%9A%E7%94%A8%E4%BA%8B%E4%BB%B6%E5%9B%9E%E8%B0%83%E6%B6%88%E6%81%AF%E4%BD%93%E6%A0%BC%E5%BC%8F%E8%AF%B4%E6%98%8E%EF%BC%9B%E4%BA%8B%E4%BB%B6%E8%AE%A2%E9%98%85&version=enterprisebase/5.0.15&blank=true
//            //https://open-icc.dahuatech.com/#/home?url=%3Fnav%3Dwiki%2Fcommon%2Ffaq.html&version=enterprisebase/5.0.15&blank=true
//            if(receiveAlarmMsg.getAlarmType() == 1000001 || receiveAlarmMsg.getAlarmType() == 300101){
//                ReceiveCarOverSpeedMsg receiveCarOverSpeedMsg = JSONUtil.toBean(receiveAlarmMsg.getExtend(), ReceiveCarOverSpeedMsg.class);
//                String alarmPicture = String.format("https://%s:%s/evo-apigw/evo-oss/%s?token=%s", platformConfig.getOssIp(), platformConfig.getOssPort(), receiveAlarmMsg.getAlarmPicture(), oauthHandler.getTokenInfo().getAccess_token());
//
//                receiveCarOverSpeedMsg.setAlarmPicture(alarmPicture);
//                log.info("收到超速信息为:{}",receiveCarOverSpeedMsg);
//                OverSpeed overSpeed = new OverSpeed(receiveCarOverSpeedMsg);
//                overSpeedInfo.insert(overSpeed);
//                //https://open-icc.dahuatech.com/#/home?url=%3Fnav%3Dwiki%2Fipms%2Fevent_list.html&version=enterprisebase/5.0.16&blank=true
//            }
//        }
//        else if(receiveMsgVO.getCategory().equals("business")){
//            if(receiveMsgVO.getMethod().equals("car.capture") || receiveMsgVO.getMethod().equals("car.access") || receiveMsgVO.getMethod().equals("vehicle.records")){
//                ReceiveCarIOMsg receiveCarIOMsg = JSONUtil.toBean(receiveMsgVO.getInfo(), ReceiveCarIOMsg.class);
//                log.info("进出车辆信息为：{}",receiveCarIOMsg);
//                Entrance entrance = new Entrance(receiveCarIOMsg);
//                entranceInfo.insertEntranceInfo(entrance);
//            }
//        }
//
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("code","0");
//        jsonObject.put("message","成功");
//        return jsonObject;
//    }
}
